package com.cnfangmao.masterdata.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.BasicDataFieldMappingDto;
import com.cnfangmao.masterdata.dto.BasicDataFieldMappingSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataFieldMapping;
import com.cnfangmao.masterdata.service.BasicDataFieldMappingService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 主数据与业务系统字段映射表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@RequestMapping("/basicDataFieldMapping")
@Api(tags="1-2、配置-主数据与业务系统字段映射管理")
@RestController
public class BasicDataFieldMappingController {
	
	private static final Logger logger =LoggerFactory.getLogger(BasicDataFieldMappingController.class);

	@Autowired
	private BasicDataFieldMappingService basicDataFieldMappingService;
	
	@PostMapping(value="/list.do")
	@ApiOperation(value="主数据与业务系统字段映列表")
	@ApiImplicitParam(name="dto",value="主数据与业务系统字段映查询条件",required=true, paramType = "body",dataType="GeneralSearchDto«BasicDataFieldMappingSearchDto»")
	public ResBody<GeneralPageVo<BasicDataFieldMapping>> listBasicDataFieldMapping(@RequestBody GeneralSearchDto<BasicDataFieldMappingSearchDto> dto) {
		ResBody<GeneralPageVo<BasicDataFieldMapping>> resBody = new ResBody<GeneralPageVo<BasicDataFieldMapping>>();
		try {
			IPage<BasicDataFieldMapping> basicDataFieldMapping=basicDataFieldMappingService.queryListBasicDataFieldMapping(dto);
			GeneralPageVo<BasicDataFieldMapping> result= new GeneralPageVo<BasicDataFieldMapping>();
			result.setPageNo(basicDataFieldMapping.getCurrent());
			result.setPageSize(basicDataFieldMapping.getPages());
			result.setRecords(basicDataFieldMapping.getRecords());
			result.setTotalCount(basicDataFieldMapping.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in BasicDataFieldMappingController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/create.do")
	@ApiOperation(value="创建主数据与业务系统字段")
	@ApiImplicitParam(name="dto",value="创建主数据与业务系统字段信息",required=true, paramType = "body",dataType="BasicDataFieldMappingDto")
	public ResBody<Boolean> createBasicDataFieldMapping(@RequestBody BasicDataFieldMappingDto dto) {
		ResBody<Boolean>  resBody =new  ResBody<Boolean>();
		try {
			if(dto.getBusinessFieldCode().isEmpty() || dto.getBusinessFieldName().isEmpty())
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("业务字段英文名称或者业务字段中文名称为空");
				return resBody;
			}
			if (basicDataFieldMappingService.findByBasicDataFieldMappingCode(dto.getBusinessFieldCode())!=null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("业务字段英文名称已经存在");
				return resBody;
			}
			if (basicDataFieldMappingService.findByBasicDataFieldMappingName(dto.getBusinessFieldName())!=null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("业务字段中文名称已经存在");
				return resBody;
			}
			dto.setMappingId(SnowFlakeUtils.generator());
			BasicDataFieldMapping basicDataFieldMapping = ModelUtils.parseObject(dto, BasicDataFieldMapping.class, "create");
			basicDataFieldMappingService.save(basicDataFieldMapping);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
			resBody.data(true);	
		} catch (Exception e) {
			logger.error(String.format("run list create in BasicDataFieldMappingController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
			
		return resBody;
	}
	
	@PostMapping(value="/update.do")
	@ApiOperation(value="修改主数据与业务系统字段")
	@ApiImplicitParam(name="dto",value="修改主数据与业务系统字段信息",required=true, paramType = "body",dataType="BasicDataFieldMappingDto")
	public ResBody<Boolean>  updateBasicDataFieldMapping(@RequestBody BasicDataFieldMappingDto dto) {
		ResBody<Boolean>  resBody =new ResBody<Boolean>();
		try {
			if(dto.getBusinessFieldCode().isEmpty() || dto.getBusinessFieldName().isEmpty())
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("业务字段英文名称或者业务字段中文名称为空");
				return resBody;
			}
			BasicDataFieldMapping businessFieldCode =basicDataFieldMappingService.findByBasicDataFieldMappingCode(dto.getBusinessFieldCode());
			if (businessFieldCode!=null && !businessFieldCode.getMappingId().equals(dto.getMappingId())) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("业务字段英文名称已经存在");
				return resBody;
			}
			BasicDataFieldMapping BusinessFieldName =basicDataFieldMappingService.findByBasicDataFieldMappingCode(dto.getBusinessFieldName());
			if (BusinessFieldName!=null && !BusinessFieldName.getMappingId().equals(dto.getMappingId())) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("业务字段中文名称已经存在");
				return resBody;
			}
			BasicDataFieldMapping basicDataFieldMapping = ModelUtils.parseObject(dto, BasicDataFieldMapping.class, "update");
			basicDataFieldMappingService.updateById(basicDataFieldMapping);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("修改成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run list update in BasicDataFieldMappingController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}
	
	@PostMapping(value="/delete.do")
	@ApiOperation(value="删除主数据与业务系统字段")
	@ApiImplicitParam(name="mappingId",value="主数据与业务系统字段 mappingId", required=true,dataType="string",paramType="body")
	public ResBody<Boolean> deleteBasicDataFieldMapping(@RequestBody String mappingId) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			basicDataFieldMappingService.removeById(mappingId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run list delete in BasicDataFieldMappingController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;		
	}
	
	@PostMapping(value="/getBasicDataFieldMapping.do")
	@ApiOperation(value="获取主数据与业务系统字段")
	@ApiImplicitParam(name="mappingId",value="主数据与业务系统字段 mappingId", required=true,dataType="string",paramType="body")
	private ResBody<BasicDataFieldMapping> getBasicDataFieldMapping(@RequestBody String mappingId) {
		ResBody<BasicDataFieldMapping> resBody =new ResBody<BasicDataFieldMapping>();
		try {
			BasicDataFieldMapping basicDataFieldMapping=basicDataFieldMappingService.getById(mappingId);
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(basicDataFieldMapping);
		} catch (Exception e) {
			logger.error(String.format("run list getBasicDataFieldMapping in BasicDataFieldMappingController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

}

